<link rel="stylesheet" href="../../layuimini/js/lay-module/step-lay/step.css" media="all">
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-body" style="padding-top: 40px;">
                    <div class="layui-carousel" id="stepForm" lay-filter="stepForm" style="margin: 0 auto;">
                        <div carousel-item>
                            <div>
                                <form class="layui-form" lay-filter="stepForm1" style="margin: 0 auto;max-width: 460px;padding-top: 40px;">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">书号:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="ID" placeholder="" class="layui-input" lay-verify="number" required/>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button class="layui-btn" lay-submit lay-filter="formStep">
                                                &emsp;下一步&emsp;
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div>
                                <form class="layui-form" lay-filter="stepForm2" style="margin: 0 auto;max-width: 460px;padding-top: 40px;">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">书号:</label>
                                        <div class="layui-input-block">
<!--                                            border:none隐藏input边框-->
                                            <input class="layui-form-mid layui-word-aux" type="number" style="border:none;" name="ID" disabled/>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">书名:</label>
                                        <div class="layui-input-block">
                                            <input class="layui-form-mid layui-word-aux" type="text" style="border:none;" name="name" disabled/>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">出版社:</label>
                                        <div class="layui-input-block">
                                            <input class="layui-form-mid layui-word-aux" type="text" style="border:none;" name="publisher" disabled/>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">剩余库存:</label>
                                        <div class="layui-input-block">
                                            <input class="layui-form-mid layui-word-aux" type="number" style="border:none;" name="book_now_num" disabled/>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">借书额度:</label>
                                        <div class="layui-input-block">
                                            <input class="layui-form-mid layui-word-aux" type="number" style="border:none;" name="user_now_num" disabled/>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button type="button" class="layui-btn layui-btn-primary pre">上一步</button>
                                            <button class="layui-btn" lay-submit lay-filter="formStep2">
                                                &emsp;确认借书&emsp;
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div>
                                <div style="text-align: center;margin-top: 90px;">
                                    <i class="layui-icon layui-circle"
                                       style="color: white;font-size:30px;font-weight:bold;background: #52C41A;padding: 20px;line-height: 80px;">&#xe605;</i>
                                    <div style="font-size: 24px;color: #333;font-weight: 500;margin-top: 30px;">
                                        借书成功
                                    </div>
                                    <div style="font-size: 14px;color: #666;margin-top: 20px;">请在两个月内归还</div>
                                </div>
                                <div style="text-align: center;margin-top: 50px;">
                                    <button class="layui-btn next">再借一本</button>
                                    <button class="layui-btn layui-btn-primary restore" layuimini-content-href="restore">已借清单</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div style="color: #666;margin-top: 30px;margin-bottom: 40px;padding-left: 30px;">
                        <h3>说明</h3><br>
                        <h4>关于线上借书</h4>
                        <p>请输入书号（书本背面的ISBN码），按照系统提示逐步完成借书。</p>
                        <br><h4>关于借书失败</h4>
                        <p>请确保书号正确，且已借书籍小于等于2本</p>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    layui.use(['form', 'step'], function () {
        var $ = layui.$,
            form = layui.form,
            step = layui.step;


        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        step.render({
            elem: '#stepForm',
            filter: 'stepForm',
            width: '100%', //设置容器宽度
            stepWidth: '750px',
            height: '500px',
            stepItems: [{
                title: '输入书号'
            }, {
                title: '确认书本信息'
            }, {
                title: '完成'
            }]
        });


        form.on('submit(formStep)', function (data) {
            const ID = data.field.ID;

            $.ajax({
                url:'/index/stu/getBookInfo'
                ,type: 'POST'
                ,data: data.field
                ,success:function (res) {
                    if(res.code == "1"){
                        const result_data = res.data;
                        step.next('#stepForm');
                        form.val('stepForm2',{
                            'ID':           ID,
                            'name':         result_data.name,
                            'publisher':    result_data.publisher,
                            'book_now_num': result_data.book_now_num,
                            'user_now_num': result_data.user_now_num
                        });
                        form.render();
                    }else if(res.code == "-1"){
                        //layer.msg是异步方法，所以后续需要同步执行的操作放到回调函数里
                        layer.msg(res.message, {icon: 2, time: 1000},function () {
                            window.location.href = '/';
                        });
                    }else{
                        layer.msg(res.message, {icon: 2});
                    }
                }
            });
            return false;
        });

        form.on('submit(formStep2)', function (data) {
            $.ajax({
                url:'/index/stu/borrowBook'
                ,type: 'POST'
                ,data: data.field
                ,success:function (res) {
                    if(res.code == "1"){
                        step.next('#stepForm');
                    }else if(res.code == "-1"){
                        //layer.msg是异步方法，所以后续需要同步执行的操作放到回调函数里
                        layer.msg(res.message, {icon: 2, time: 1000},function () {
                            window.location.href = '/';
                        });
                    }else{
                        layer.msg(res.message, {icon: 2});
                    }
                }
            });
            return false;
        });

        $('.pre').click(function () {
            step.pre('#stepForm');
        });

        $('.next').click(function () {
            step.next('#stepForm');
        });
    })
</script>
